package com.authsphere.security.common.handler;

import com.authsphere.common.result.ResponseResult;
import com.authsphere.common.result.ResponseResultUtils;
import com.authsphere.security.common.logging.LoggingAuthenticationFailureHandler;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/25 21:35
 **/
public class ResponseResultAuthenticationFailureHandler extends LoggingAuthenticationFailureHandler {

    @Override
    protected void doAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
        if (!response.isCommitted()) {
            HttpStatus httpStatus = HttpStatus.UNAUTHORIZED;
            ResponseResultUtils.result(response, ResponseResult.ofFailed0(httpStatus.value(), exception.getMessage()));
        }
    }
}
